package com.sy.core.service;

import com.sy.core.mapper.SyShoppingCartMapper;
import com.sy.core.model.SyShoppingCart;
import com.sy.core.util.NumUtils;
import com.sy.core.web.BaseCtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCartService extends BaseCtl {

    @Autowired
    private SyShoppingCartMapper syShoppingCartMapper;

    public String addAndEdit(SyShoppingCart syShoppingCart) {
        String pk = null;
        //无论是新增还是修改是时候先通过skupk查询此商品是否已经添加过购物车，如果添加过购物车那么在原有的基础上修改就行
        List<Map<String, Object>> mapList = syShoppingCartMapper.queryList(syShoppingCart);
        if(mapList!=null&&mapList.size()>0){
            pk=(String)mapList.get(0).get("pk");
            syShoppingCart.setPk(pk);
        }
        if(pk!=null){
            syShoppingCartMapper.updateByPrimaryKeySelective(syShoppingCart);
            SyShoppingCart syShoppingCart2 = syShoppingCartMapper.selectByPrimaryKey(syShoppingCart.getPk());
            //return Integer.parseInt(syShoppingCart2.getProductSkuCount());
        }else{
            pk=getPrimaryKey();
            syShoppingCart.setPk(pk);
            syShoppingCart.setCreateBy(getLoginUser().getPk());
            syShoppingCart.setCreateTime(getTime());
            syShoppingCartMapper.insertSelective(syShoppingCart);
        }
        return pk;
    }

    /**
     * 把总价计算完以后再给到前端
     * @param syShoppingCart
     * @return
     */
    public List<Map<String,Object>> queryList(SyShoppingCart syShoppingCart) {
        syShoppingCart.setShoppingCartUserpk(getLoginUser().getPk());
        List<Map<String,Object>> syShoppingCartList = syShoppingCartMapper.queryList(syShoppingCart);
        if(syShoppingCartList!=null&&syShoppingCartList.size()>0){
            String  productSkuPk = null;//skuPk
            String  productSkuCount=null;//数量
            String  productOffPrice=null;//产品折后价
            String  skuOffPrice=null;//sku折后价
            String totalPrice=null;//总价
            for (int i = 0; i <syShoppingCartList.size() ; i++) {
                productSkuPk=(String)syShoppingCartList.get(i).get("product_sku_pk");
                productSkuCount=(String)syShoppingCartList.get(i).get("product_sku_count");
                if(productSkuPk.equals("-1")){//说明此产品没有sku，通过产品折后价来计算
                    productOffPrice=(String)syShoppingCartList.get(i).get("product_sale");
                    syShoppingCartList.get(i).put("sku_off_price",productOffPrice);
                    totalPrice= new BigDecimal(productSkuCount).multiply(new BigDecimal(productOffPrice.replaceAll(",",""))).toString();
                }else{
                    skuOffPrice=(String)syShoppingCartList.get(i).get("sku_off_price");
                    totalPrice= new BigDecimal(productSkuCount).multiply(new BigDecimal(skuOffPrice.replaceAll(",",""))).toString();
                }
                totalPrice= NumUtils.parseMoney(totalPrice);//把字符串转换成货币类型的字符串
                syShoppingCartList.get(i).put("total_price",totalPrice);
            }
        }
        return  syShoppingCartList;
    }

    public Integer queryCount(SyShoppingCart syShoppingCart) {
        return syShoppingCartMapper.queryCount(syShoppingCart);
    }

    public Integer batchDel(String pks) {
        return syShoppingCartMapper.batchDel(pks);
    }

    /**
     * 把计算完的总价给到前端
     * @param pk
     * @return
     */
    public Map<String,Object> queryOne(String pk) {
        Map<String, Object> shoppingCart = syShoppingCartMapper.queryOne(pk);
        String  productSkuPk = (String)shoppingCart.get("product_sku_pk");//skuPk
        String  productSkuCount=(String)shoppingCart.get("product_sku_count");//数量
        String  productOffPrice=(String)shoppingCart.get("product_sale");//产品折后价
        String  skuOffPrice=(String)shoppingCart.get("sku_off_price");//sku折后价
        String totalPrice=null;//总价
        if(productSkuPk.equals("-1")){//说明此产品没有sku，通过产品折后价来计算
            shoppingCart.put("sku_off_price",productOffPrice);
            totalPrice= new BigDecimal(productSkuCount).multiply(new BigDecimal(productOffPrice.replaceAll(",",""))).toString();
        }else{
            totalPrice= new BigDecimal(productSkuCount).multiply(new BigDecimal(skuOffPrice.replaceAll(",",""))).toString();
        }
        totalPrice= NumUtils.parseMoney(totalPrice);//把字符串转换成货币类型的字符串
        shoppingCart.put("total_price",totalPrice);
        return shoppingCart;
    }
}
